package com.example.tiantian.process;

import java.util.List;
import java.util.Set;

import com.example.tiantian.R;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Debug.MemoryInfo;
import android.text.format.Formatter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ProcessAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    List<RunningAppProcessInfo> list;
    Set<RunningAppProcessInfo> checked;
    PackageManager packManager;
    ActivityManager actManager;

    public ProcessAdapter(Context context, List<RunningAppProcessInfo> list, Set<RunningAppProcessInfo> checked,
            ActivityManager actManager, PackageManager packManager) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
        this.checked = checked;

        this.actManager = actManager;
        this.packManager = packManager;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public RunningAppProcessInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        class Views {
            ImageView imgIcon;
            TextView txtName;
            TextView txtSize;
            CheckBox cbCheck;

            Views(View v) {
                cbCheck = (CheckBox) v.findViewById(R.id.process_item_check);
                imgIcon = (ImageView) v.findViewById(R.id.process_item_icon);
                txtName = (TextView) v.findViewById(R.id.process_item_name);
                txtSize = (TextView) v.findViewById(R.id.process_item_size);
            }
        }
        Views views = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.process_item, null);
            views = new Views(convertView);
            convertView.setTag(views);
        } else {
            views = (Views) convertView.getTag();
        }
        views.cbCheck.setVisibility(View.VISIBLE);
        try {
            RunningAppProcessInfo r = list.get(position);
            if (r.processName.equals("android.process.acore") || r.processName.equals("android.process.media")) {
                // 两个特殊的系统进程
                views.imgIcon.setImageResource(R.drawable.ic_launcher);
                views.txtName.setText(r.processName);
            } else {
                // processname--->应用信息--》获取应用图标 短名
                ApplicationInfo app;

                app = packManager.getApplicationInfo(r.processName, 0);
                views.imgIcon.setImageDrawable(app.loadIcon(packManager));
                String name = (String) app.loadLabel(packManager);
                if (name == null) {
                    name = app.packageName;
                }
                if (name == null) {
                    name = app.processName;
                }
                views.txtName.setText(name);
            }

            // 获取每个进程所占用的内存
            MemoryInfo[] minfos = actManager.getProcessMemoryInfo(new int[] { r.pid });
            MemoryInfo minfo = minfos[0];
            String desc = Formatter.formatFileSize(context, minfo.getTotalSharedDirty() * 1024);
            views.txtSize.setText(desc);

            // 这个选项是代表本身应用 应该隐藏其中的checkbox
            if (r.processName.equals(context.getPackageName())) {
                views.cbCheck.setVisibility(View.INVISIBLE);
            }

            // 每个checkBox是否是被选中的 需要判断：当前r 是不是在choosedPros内
            views.cbCheck.setChecked(checked.contains(r));

        } catch (NameNotFoundException e) {
            Log.e(this.getClass().getName(), e.toString());
        }
        return convertView;
    }

    public void setList(List<RunningAppProcessInfo> list) {
        this.list = list;
    }

}
